package com.ltmonitor.web.controller.command;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.BasicData;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.entity.VehicleRecorder;
import com.ltmonitor.service.IMediaItemService;
import com.ltmonitor.service.ITerminalParamService;
import com.ltmonitor.service.IVehicleRecorderService;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.util.ClassInstantiateException;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.vo.RecorderParam;

/**
 * 车辆行驶记录仪查询
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/vehicleRecorder")
public class VehicleRecorderAction extends TerminalCommandAction {


	@Autowired
	protected IMediaItemService mediaItemService;
	
	@Autowired
	protected IVehicleRecorderService vehicleRecorderService;


	@RequestMapping("/view.action")
	public String view(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/command/vehicleRecorder";
	}


	// 获取命令下发的结果

	@ResponseBody
	@RequestMapping("/queryResult.action")
	public JsonMessage queryResult(long commandId) {
		TerminalCommand tc = getTerminalCommand(commandId);
		if (TerminalCommand.STATUS_SUCCESS.equals(tc.getStatus()) == false) {
			BasicData bd = this.getBasicDataService().getBasicDataByCode(
					tc.getStatus(), "TCommandStatus");
			String msg = bd != null ? bd.getName() : "";
			return json(false, msg);
		}
		String hsql = "from VehicleRecorder where commandId = ?";
		List result = this.vehicleRecorderService.query(hsql, commandId);
		if (result.size() > 0) {
			Map resultMap = new HashMap();
			int cmd = Integer.parseInt(tc.getCmd());
			if (cmd == 1) {
				VehicleRecorder vr = (VehicleRecorder) result.get(0);
				String data = vr.getCmdData();
				String[] strData = data.split(",");
				if (strData.length >= 2) {
					resultMap.put("driverNo", strData[0]);
					resultMap.put("driverLicense", strData[1]);
				}
			} else if (cmd == 2) {
				VehicleRecorder vr = (VehicleRecorder) result.get(0);
				String data = vr.getCmdData();
				// String[] strData = data.split(",");.
				resultMap.put("clock", data);
			} else if (cmd == 4) {
				VehicleRecorder vr = (VehicleRecorder) result.get(0);
				String data = vr.getCmdData();
				// String[] strData = data.split(",");.
				resultMap.put("feature", data);
			} else if (cmd == 6) {
				VehicleRecorder vr = (VehicleRecorder) result.get(0);
				String data = vr.getCmdData();
				String[] strData = data.split(",");
				if (strData.length == 3) {
					resultMap.put("vin", strData[0]);
					resultMap.put("vehicleNo", strData[1]);
					resultMap.put("vehicleType", strData[2]);
				}
			}
			return json(true, resultMap);

		}
		return json(false, result);
	}

	@ResponseBody
	@RequestMapping("/queryRecorderResult.action")
	public JsonMessage  queryRecorderResult(long commandId) {
		TerminalCommand tc = getTerminalCommand(commandId);
		if (TerminalCommand.STATUS_SUCCESS.equals(tc.getStatus()) == false) {
			return json(false, "");
		}
		List result = this.mediaItemService.queryMediaItemListByCommandId(commandId);
		return json(true, result);
	}
	
	
	/**
	 * 行车记录仪设置和数据采集
	 */

	@ResponseBody
	@RequestMapping("/send.action")
	public JsonMessage  execute( long vehicleId,String cmdType, String operation,Date startDate,Date endDate,String ver) {
		
		// 数据采集
		if ("query".equals(operation)) {
			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_VEHICLE_RECORDER);

			Integer cmd = JT808Constants.getRecorderCmd(cmdType);
			if (cmd != null) {
				tc.setCmd("" + cmd);
				tc.setCmdData("" + cmd + ";" + startDate + ";" + endDate);
			}
			this.SendCommand(tc,vehicleId);
			return json(true, tc);
		}

		Map parameters = super.getParams();
		RecorderParam rp = new RecorderParam();
		try {
			BeanUtils.copyProperties(rp, parameters);

			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_VEHICLE_RECORDER_CONFIG);

			Integer cmd = JT808Constants.getRecorderCmd("set" + cmdType);
			if (cmd != null) {
				tc.setCmd("" + cmd);
			}
			StringBuilder sb = new StringBuilder();
			if ("vehicleInfo".equals(cmdType)) {// 存储的车辆VIN号、车牌号码、分类
				sb.append(cmd).append(";").append(rp.getVin()).append(";")
						.append(rp.getVehicleNo()).append(";")
						.append(rp.getVehicleType()).append(";");
			} else if ("clock".equals(cmdType)) {
				sb.append(cmd).append(";").append(rp.getClock());
			} else if ("driverInfo".equals(cmdType)) {
				sb.append(cmd).append(";").append(rp.getDriverNo()).append(";")
						.append(rp.getDriverLicense());
			} else if ("feature".equals(cmdType)) {
				sb.append(cmd).append(";").append(rp.getFeature());
			}
			tc.setCmdData(sb.toString());
			this.SendCommand(tc,vehicleId);
			return json(true, tc);

		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return json(false, e.getMessage());
		}
	}


}
